package org.java8newfeature.optional;

import java.util.Optional;
import java.util.function.Supplier;

public class OptionalDemo {
    public static void main(String[] args) {

        User user = new User("张三");

        Optional<User> optional = Optional.of(user);
        // TODO: 2024/6/28  上述等同于下列
        optional.ifPresent(e -> e.eat("asdfasdf"));

        User u = null;

        User lisi = Optional.ofNullable(u).orElse(new User("李四"));
        lisi.eat("香蕉");


        User user1 = Optional.ofNullable(lisi).orElseGet(() -> new User("wangwu"));
        user1.eat("西瓜");

        // TODO: 2024/6/28 等价下列
        /*User user1= Optional.ofNullable(lisi).orElseGet(new Supplier<User>() {
            @Override
            public User get() {
                return new User("wangwu");
            }
        });*/
        Optional<User> u1 = Optional.ofNullable(u);
        u1.get();
    }

}
